package com.polylink.${bean.name}.dao.sqlProvider;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.polylink.base.Page;

/**
 *@author xiliang.xiao
 *@date 2015年3月23日 上午9:53:20
 *
 **/
public class ${bean.upName}SqlProvider {
	private static String ${bean.name}_table = "${bean.table} u";

	public String list(Page p){
		BEGIN();
		SELECT("<#list bean.attrs as attr>u.${attr.dbName} ${attr.name}<#if attr_has_next>,</#if></#list>");
		FROM(${bean.name}_table);
		conditions(p.getQuery());
		//ORDER_BY(columns);
		return SQL()+" limit ${r"#{begin},#{pageSize}"}";
	}
	
	public String count(Page p){
		BEGIN();
		SELECT("count(1)");
		FROM(${bean.name}_table);
		conditions(p.getQuery());
		return SQL();
	}
	
	public void conditions(Map<String,Object> query){
		<#list bean.attrs as attr>
		String ${attr.name} = (String) query.get("${attr.name}");
		</#list>
		
		<#list bean.attrs as attr>
		if(StringUtils.isNotBlank(${attr.name})){
			WHERE("u.${attr.dbName} = ${r"#{query."}${attr.name}${r"}"}");
		}
		</#list>
	}
}
